package com.daiqee.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daiqee.pojo.ProcurementPlan;
import com.daiqee.pojo.ProcurementProduction;
import com.daiqee.pojo.ProductionPrise;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.daiqee.dao.mapper.ProcurementPlanMapper;
import com.daiqee.dao.mapper.ProcurementProductionMapper;

/**
 * 采购业务
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月8日 上午8:24:53
 * @package com.daiqee.service.impl
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Service
public class ProcurementPlanService extends ServiceImpl<ProcurementPlanMapper, ProcurementPlan>{

	@Autowired
	private ProcurementPlanMapper procurementPlanMapper;
	
	@Autowired
	private ProcurementProductionMapper procurementProductionMapper;
	
	/**
	 * 添加 采购计划
	 * 
	 * 解释下 为什么需要在service层对数据进行处理,而不再Controller层
	 * 如果procurementPlanMapper.insert(procurementPlan)执行的过程中出现异常就直接回滚
	 * 那么Controller层对procureProd 数据的封装将造成浪费。所以在Service直接就回滚
	 * 
	 * @param procurementPlan
	 * @return true:添加成功,false:添加失败
	 */
	public Integer addProcurementPlan(ProcurementPlan procurementPlan) {
		//添加审核状态
		procurementPlan.setProcurePlanCheckStatus("1");
		//添加采购状态
		procurementPlan.setProcurePlanBuyerStatus("1");
		//添加到采购计划表
		procurementPlanMapper.insert(procurementPlan);
		//创建一个实体用来添加数据
		ProcurementProduction procureProd = new ProcurementProduction();
		//封装数据
		procureProd.setProcurePlanId(procurementPlan.getProcurePlanId());
		//存储商品id
		String tempProdId = "";
		//存储商品数量
		String tempProdBuyCount = "";
		//存储产品的总价格 数量*单价
		String tempTotalPrise = "";
		//先取出计划中存放的产品
		ProductionPrise[] productions = procurementPlan.getData();
		//对数据进行处理,已 id,id,id 这种字符串格式存储
		for(int index = 0;index<productions.length;index++) {
			tempProdId+= productions[index].getProdId()+"-";
			tempProdBuyCount+= productions[index].getBuyCount()+"-";
			tempTotalPrise+=
					productions[index].getBuyCount()*productions[index].getProdPrice()+"-";
		}
		//数据添加到对象中
		procureProd.setProdId(tempProdId);
		procureProd.setProdBuyCount(tempProdBuyCount);
		procureProd.setProdTotalMoney(tempTotalPrise);
		//去任意一个data即可,因为都属于同一个供应商
		procureProd.setSupArchiId(procurementPlan.getData()[0].getProdSupplierId());
		//添加采购和产品对应表,插入对象
		Integer resultNumber = procurementProductionMapper.insert(procureProd);
		return resultNumber;
	}

	/**
	 * 删除 采购计划
	 * @param idList
	 * 			需要被删除的id号
	 * 
	 * @return 受影响的数量
	 */
	public Integer removeProcurementPlans(List<String> idList) {
		//删除采购计划表中的记录
		procurementPlanMapper.deleteBatchIds(idList);
		//删除关系表中的记录
		return procurementProductionMapper.deleteBatchIds(idList);
	}

	/**
	 * 根据 条件查询
	 * @param procurementPlan
	 * @return 符合条件的结果
	 */
	public List<ProcurementPlan> queryProcurementPlan(ProcurementPlan plan) {
		List<ProcurementPlan> planList = procurementPlanMapper.queryProcurementPlan(plan);
		return planList;
	}

	/**
	 * 更新 采购计划
	 * @param procurementPlan
	 * 				采购计划实体
	 * @return 受影响的行数
	 */
	public Integer updateProcurementPlan(ProcurementPlan procurementPlan) {
		//添加采购和产品对应表,插入对象
		Integer number = procurementPlanMapper.updateById(procurementPlan);
		
		if(procurementPlan.getData() != null) {
			//创建一个实体用来添加数据
			ProcurementProduction procureProd = new ProcurementProduction();
			//封装数据
			procureProd.setProcurePlanId(procurementPlan.getProcurePlanId());
			//存储商品id
			String tempProdId = "";
			//存储商品数量
			String tempProdBuyCount = "";
			//存储产品的总价格 数量*单价
			String tempTotalPrise = "";
			//先取出计划中存放的产品
			ProductionPrise[] productions = procurementPlan.getData();
			//对数据进行处理,已 id,id,id 这种字符串格式存储
			for(int index = 0;index<productions.length;index++) {
				tempProdId+= productions[index].getProdId()+"-";
				tempProdBuyCount+= productions[index].getBuyCount()+"-";
				tempTotalPrise+=
						productions[index].getBuyCount()*productions[index].getProdPrice()+"-";
			}
			//数据添加到对象中
			procureProd.setProdId(tempProdId);
			procureProd.setProdBuyCount(tempProdBuyCount);
			procureProd.setProdTotalMoney(tempTotalPrise);
			//取任意一个data即可,因为都属于同一个供应商
			procureProd.setSupArchiId(procurementPlan.getData()[0].getProdSupplierId());
			procurementProductionMapper.updateById(procureProd);
		}
			
		return number;
	}

}
